package com.hupu.jrs.ui.browser

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.support.v4.view.MenuItemCompat
import android.support.v7.widget.ShareActionProvider
import android.support.v7.widget.Toolbar
import android.view.Menu
import android.view.MenuItem
import butterknife.BindView
import butterknife.ButterKnife
import com.hupu.jrs.R
import com.hupu.jrs.ui.BaseSwipeBackActivity
import com.hupu.jrs.util.StringUtil

/**
 * Created by sll on 2016/3/11.
 */
class BrowserActivity : BaseSwipeBackActivity() {

    @BindView(R.id.toolbar)
    internal var toolbar: Toolbar? = null


    private var url: String? = null
    private var mFragment: BrowserFragment? = null

    protected override val isApplyStatusBarTranslucency: Boolean
        get() = true

    protected override val isApplyStatusBarColor: Boolean
        get() = true

    override fun initContentView(): Int {
        return R.layout.base_content_toolbar_layout
    }

    override fun initInjector() {

    }

    override fun initUiAndListener() {
        ButterKnife.bind(this)
        initToolBar(toolbar)
        url = getIntent().getStringExtra("url")
        mFragment = BrowserFragment.newInstance(url!!, "", getIntent().getBooleanExtra("external", true))
        getFragmentManager().beginTransaction().replace(R.id.content, mFragment).commit()
    }

    override fun onPrepareOptionsMenu(menu: Menu): Boolean {
        menu.removeGroup(R.id.browser)
        getMenuInflater().inflate(R.menu.menu_browser, menu)

        val shareContent = String.format("%s %s ",  getTitle().toString() + "", url!! + "")
        val shareIntent = Intent(Intent.ACTION_SEND)
        shareIntent.type = "text/plain"
        shareIntent.putExtra(Intent.EXTRA_TEXT, shareContent)

        val shareItem = menu.findItem(R.id.share)
        val shareProvider = MenuItemCompat.getActionProvider(shareItem) as ShareActionProvider
        shareProvider.setShareHistoryFileName("channe_share.xml")
        shareProvider.setShareIntent(shareIntent)

        return super.onPrepareOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == R.id.refresh) {
            if (mFragment != null) {
                mFragment!!.reload()
            }
        } else if (item.itemId == R.id.copy) {
            StringUtil.copy(this, url!!)
        } else if (item.itemId == R.id.to_browser) {
            try {
                val intent = Intent()
                intent.action = "android.intent.action.VIEW"
                val content_url = Uri.parse(url)
                intent.data = content_url
                startActivity(intent)
            } catch (e: Exception) {
            }

        }
        return super.onOptionsItemSelected(item)
    }

    companion object {

        @JvmOverloads
        fun startActivity(mContext: Context, url: String, external: Boolean = true) {
            val intent = Intent(mContext, BrowserActivity::class.java)
            intent.putExtra("url", url)
            intent.putExtra("external", external)
            mContext.startActivity(intent)
        }
    }
}
